package me.heyclock.hl.recordwriter.app;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.nukc.stateview.StateView;

import me.heyclock.hl.recordwriter.R;
import me.heyclock.hl.recordwriter.controller.BaseView;
import me.heyclock.hl.recordwriter.ui.RotateAnimatorProvider;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * Created by hl on 2018/4/27.
 */

public class BaseSwipeBackTitleActivity extends SwipeBackActivity implements BaseView {

    private ImageView leftIv;
    private TextView titleTv;
    private LinearLayout contentLayout;
    private View contentView;
    private onClickEvent interfaceOnClick;

    /**
     * 进度条
     */
    private StateView stateView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ///< 共用标题栏
        setContentView(R.layout.activity_base_swipe_back_common_title);
        leftIv = (ImageView) findViewById(R.id.absbct_settingBack);
        leftIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != interfaceOnClick){
                    interfaceOnClick.onBackClickEvent();
                }
                else{
                    finish();
                }
            }
        });
        titleTv = (TextView) findViewById(R.id.absbct_title);
        contentLayout = (LinearLayout) findViewById(R.id.absbct_functionRoot);
    }

    /**
     * 设置内容区域
     * @param resId
     */
    public void setContentLayout(int resId){
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = layoutInflater.inflate(resId, null);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        contentView.setLayoutParams(layoutParams);
        if (null != contentLayout){
            contentLayout.addView(contentView);
        }
    }

    /***
     * 设置内容区域
     *
     * @param view View对象
     */
    public void setContentLayout(View view) {
        if (null != contentLayout) {
            contentLayout.addView(view);
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        if (null != titleTv) {
            titleTv.setText(title);
        }
    }

    /**
     * 设置标题
     * @param resId
     */
    public void setTitle(int resId){
        titleTv.setText(getString(resId));
    }

    /**
     * 设置左侧图标
     * @param resId
     */
    public void setLeftIcon(int resId){
        leftIv.setImageResource(resId);
    }

    /**
     * 设置点击事件监听
     * @param interfaceOnClick
     */
    public void setOnclickEvent(onClickEvent interfaceOnClick){
        this.interfaceOnClick = interfaceOnClick;
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * des: 设置进度条
     * author: hl
     * @param activity
     */
    protected void setStateView(Activity activity){
        ///< 进度条
        stateView = StateView.inject(activity);
        ///< 自定义了布局，那么自定义一下动画吧...
        stateView.setAnimatorProvider(new RotateAnimatorProvider());
    }

    /**
     * des: 设置进度条+重试监听
     * author: hl
     * @param activity
     * @param onRetryClickListener
     */
    protected void setStateView(Activity activity, StateView.OnRetryClickListener onRetryClickListener){
        ///< 进度条
        stateView = StateView.inject(activity);
        ///< 自定义了布局，那么自定义一下动画吧...
        stateView.setAnimatorProvider(new RotateAnimatorProvider());
        ///< 设置重试事件
        stateView.setOnRetryClickListener(onRetryClickListener);
    }

    /**
     * des: 设置进度条+重试监听
     * author: hl
     * @param view
     * @param onRetryClickListener
     */
    protected void setStateView(View view, StateView.OnRetryClickListener onRetryClickListener){
        ///< 进度条
        stateView = StateView.inject(view);
        ///< 自定义了布局，那么自定义一下动画吧...
        stateView.setAnimatorProvider(new RotateAnimatorProvider());
        ///< 设置重试事件
        stateView.setOnRetryClickListener(onRetryClickListener);
    }

    /**
     * des: 设置进度条
     * author: hl
     * @param view
     */
    protected void setStateView(View view){
        ///< 进度条
        stateView = StateView.inject(view);
        ///< 自定义了布局，那么自定义一下动画吧...
        stateView.setAnimatorProvider(new RotateAnimatorProvider());
    }

    /**
     * 显示进度，目前搜索页面会用到这个接口
     */
    protected void loadStateView(){
        stateView.showLoading();
    }

    /**
     * 显示内容，目前搜索页面会用到这个接口
     */
    protected void showStateView(){
        stateView.showContent();
    }

    /**
     * 显示空内容，目前搜索页面会用到这个接口
     */
    protected void emptyStateView(){
        stateView.showEmpty();
    }

    /**
     * 显示重试内容，目前搜索页面会用到这个接口
     */
    protected void retryStateView(){
        stateView.showRetry();
    }

    @Override
    public void showDialog() {
        stateView.showLoading();
    }

    @Override
    public void disDialog() {
        stateView.showContent();
    }

    @Override
    public void retryDialog() {
        stateView.showRetry();
    }

    @Override
    public void emptyDialog() {
        stateView.showEmpty();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    /**
     * 点击事件回调接口
     */
    public interface onClickEvent{
        public void onBackClickEvent();
    }
}
